/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import item.Item;
import javax.swing.JOptionPane;
import myJPA.Model;

/**
 *
 * @author nacerkhalil
 */
public class AddCopy extends javax.swing.JPanel {

    /**
     * Creates new form AddCopy
     */
    public AddCopy() {
        initComponents();
        resultNotFoundLabel.setVisible(false);
        jPanel1.setVisible(false);
        invalidInputLabel.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        resultNotFoundLabel = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        addCopies = new javax.swing.JButton();
        invalidInputLabel = new javax.swing.JLabel();
        removeButton = new javax.swing.JButton();

        jLabel1.setText("Enter movie or video game ID:");

        resultNotFoundLabel.setForeground(new java.awt.Color(255, 0, 0));
        resultNotFoundLabel.setText("No Item is associated with the ID");

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Enter number of copies:");

        addCopies.setText("Add");
        addCopies.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCopiesActionPerformed(evt);
            }
        });

        invalidInputLabel.setForeground(new java.awt.Color(255, 0, 0));
        invalidInputLabel.setText("Invalid input");

        removeButton.setText("remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(37, 37, 37)
                .add(jLabel2)
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(invalidInputLabel)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 59, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(addCopies, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(24, 24, 24)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(addCopies))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(invalidInputLabel)
                    .add(removeButton))
                .addContainerGap(72, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(29, 29, 29)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(resultNotFoundLabel)
                            .add(layout.createSequentialGroup()
                                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 239, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(searchButton)))))
                .addContainerGap(240, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(49, 49, 49)
                .add(resultNotFoundLabel)
                .add(29, 29, 29)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(searchButton))
                .add(29, 29, 29)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(155, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try {
            resultNotFoundLabel.setVisible(false);
            
            int id = Integer.parseInt(jTextField1.getText());
            item = Model.getEntityManager().find(Item.class, id);
            
            jPanel1.setVisible(true);
            

            
            
            jTextField1.setText("");
        } catch (Exception ex) {
            item = null;
            resultNotFoundLabel.setVisible(true);
            
        }
    }//GEN-LAST:event_searchButtonActionPerformed

    private void addCopiesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCopiesActionPerformed

        try
        {
            if(item != null)
            {
                int copies = Integer.parseInt(jTextField2.getText());
                item.setNumberCopies(item.getAvailableCopies() + copies);
                item.setAvailableCopies(item.getAvailableCopies() + copies);
                item.update();
                JOptionPane.showMessageDialog(null, "There are now "+item.getNumberCopies()+" copies");
            }
        }
        catch(Exception ex)
        {
            invalidInputLabel.setVisible(true);
        }
    }//GEN-LAST:event_addCopiesActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        try
        {
            if(item != null)
            {
                int copies = Integer.parseInt(jTextField2.getText());
                if(item.getAvailableCopies() < copies)
                {
                    JOptionPane.showMessageDialog(null, "There are only "+item.getAvailableCopies()+ " in the stock, the rest is rented");
                    throw new Exception();
                }
                
                item.setNumberCopies(item.getAvailableCopies() - copies);
                item.setAvailableCopies(item.getAvailableCopies() - copies);
                item.update();
                JOptionPane.showMessageDialog(null, "There are now "+item.getNumberCopies()+" copies");
            }
        }
        catch(Exception ex)
        {
            invalidInputLabel.setVisible(true);
        }
    }//GEN-LAST:event_removeButtonActionPerformed
    private Item item;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCopies;
    private javax.swing.JLabel invalidInputLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel resultNotFoundLabel;
    private javax.swing.JButton searchButton;
    // End of variables declaration//GEN-END:variables
}
